﻿using System;
using System.Collections.Generic;

using System.Data.SqlClient;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace els_web
{
    public partial class _Default : System.Web.UI.Page
    {
        public List<izhodi_> izhodi = new List<izhodi_>();
        public List<operaterji_> operaterji = new List<operaterji_>();
        public List<statusi_> statusi = new List<statusi_>();
        public List<napake_> napake = new List<napake_>();

        protected void Page_Load(object sender, EventArgs e)
        {


            DataView view = (DataView)SqlDataSource5.Select(new DataSourceSelectArguments());
            DataTable groupsTable = view.ToTable();
            foreach (DataRow dr in groupsTable.Rows)
            {
                operaterji.Add(new operaterji_(Convert.ToInt32(dr[0]), dr[1].ToString() + " " + dr[2].ToString()));
            }

            view = (DataView)SqlDataSource7.Select(new DataSourceSelectArguments());
            groupsTable = view.ToTable();
            foreach (DataRow dr in groupsTable.Rows)
            {
                statusi.Add(new statusi_(Convert.ToInt32(dr[0]), dr[1].ToString()));
            }

            view = (DataView)SqlDataSource8.Select(new DataSourceSelectArguments());
            groupsTable = view.ToTable();
            foreach (DataRow dr in groupsTable.Rows)
            {
                napake.Add(new napake_(Convert.ToInt32(dr[0]), dr[1].ToString(), dr[2].ToString()));
            }
        }

        public struct izhodi_
        {
            public string procesna;
            public CheckBox status_izhod;

            public izhodi_(string procesna_, CheckBox status_izhod_)
            {
                procesna = procesna_;
                status_izhod = status_izhod_;
            }
        }
 
        public struct statusi_
        {
            public int id;
            public string opis;

            public int ID { get { return id; } set { id = value; } }

            public statusi_(int id_, string opis_)
            {
                id = id_;
                opis = opis_;
            }
        }

        public struct napake_
        {
            public int id;
            public string opis;
            public string koda;

            public int ID { get { return id; } set { id = value; } }

            public napake_(int id_, string opis_, string koda_)
            {
                id = id_;
                opis = opis_;
                koda = koda_;
            }
        }

        public struct operaterji_
        {
            public int id;
            public string ime;

            public int ID { get { return id; } set { id = value; } }

            public operaterji_(int id_, string ime_)
            {
                id = id_;
                ime = ime_;
            }
        }

        protected void RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label xx = e.Row.FindControl("operacija") as Label;
                e.Row.Attributes.Add("onMouseOver", "Highlight(" + xx.ToolTip + ")");
                e.Row.Attributes.Add("onMouseOut", "UnHighlight(" + xx.ToolTip + ")");
            }
        }
        // ikonce za status operacije
        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {

            string procesna = e.Row.Cells[0].Text;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                SqlDataSource3.SelectCommand =
                    "select * from KAKOVOST.dbo.SPREMNILISTIZDELKA as a inner join (select OPERACIJA, count(OPERACIJA) as STEVILOCIKLOV, count(case when REZULTAT=2 THEN 1 END) as PROBLEMI, max(DATUM) as datum from KAKOVOST.dbo.SPREMNILISTIZDELKA WHERE (PROCESNA = '" + procesna + "') GROUP BY OPERACIJA) as b on a.DATUM = b.datum and PROCESNA = '" + procesna + "'";
                
                //"select OPERACIJA, count(OPERACIJA), count(case when REZULTAT=2 THEN 1 END), max(DATUM) as datum from KAKOVOST.dbo.SPREMNILISTIZDELKA WHERE (PROCESNA = '" + procesna + "') GROUP BY OPERACIJA ORDER BY datum ASC;";
                DataView view = (DataView)SqlDataSource3.Select(new DataSourceSelectArguments());
                DataTable groupsTable = view.ToTable();
                String operacija;
                String stevilo;
                int napak;
                foreach (DataRow dr in groupsTable.Rows)    
                {
                    //Do something here IE grab the value of the first column
                    operacija = dr[5].ToString();
                    stevilo = dr[10].ToString();
                    napak = Convert.ToInt32(dr[11]);
                    ImageButton operacijabtn = new ImageButton();
                    operacijabtn.ImageAlign = ImageAlign.Middle;
                    operacijabtn.ImageUrl = "controls_button.aspx?t=" + operacija + "&n=" + stevilo;
                    if (Convert.ToInt32(dr[7]) > 1)
                        operacijabtn.ImageUrl += "&m=0";
                    if (Convert.ToInt32(dr[7]) == 5)
                        operacijabtn.ImageUrl += "&k=0";
                    if (napak != 0)
                        operacijabtn.ImageUrl += "&w=!";
                    operacijabtn.CommandArgument = e.Row.Cells[0].Text;
                    operacijabtn.Attributes.Add("onclick", "javascript: return redirect('" + operacijabtn.CommandArgument + "')");
                    if (operacija != string.Empty)
                        e.Row.Cells[2].Controls.Add(operacijabtn);
                    if (Convert.ToInt32(dr[7]) == 1)
                    {
                        ((CheckBox)e.Row.Cells[4].Controls[1]).Checked = true;
                    }
                    else
                    {
                        ((CheckBox)e.Row.Cells[4].Controls[1]).Checked = false;
                    }
                }

                //SqlDataSource9.SelectParameters["procesna"].DefaultValue = e.Row.Cells[0].Text;
                //view = (DataView)SqlDataSource9.Select(new DataSourceSelectArguments());
                //groupsTable = view.ToTable();
                //if (groupsTable.Rows.Count > 0)
                //{
                //    ((CheckBox)e.Row.Cells[4].Controls[1]).Checked = true;
                //}
                //else
                //{
                //    ((CheckBox)e.Row.Cells[4].Controls[1]).Checked = false;
                //}

                SqlDataSource10.SelectParameters["Card_Bar_Code"].DefaultValue = e.Row.Cells[0].Text;
            }

            if (e.Row.RowType == DataControlRowType.Header)
            {
                //Find the checkbox control in header and add an attribute
                ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" +
                        ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");
            }
        }




        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataView view = (DataView)SqlDataSource4.Select(new DataSourceSelectArguments());
                DataTable groupsTable = view.ToTable();
                foreach (DataRow dr in groupsTable.Rows)
                {
                    Label1.Text = dr[0].ToString();

                }
                izhodi = new List<izhodi_>();
            }
            catch { }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            foreach (GridViewRow row in GridView1.Rows)
            {
                string procesna = row.Cells[0].Text;
                bool izhod = ((CheckBox)row.Cells[4].Controls[1]).Checked;

                SqlDataSource9.UpdateCommand = SqlDataSource9.UpdateCommand.Replace("@PROCESNA", "'" + procesna + "'");
                SqlDataSource9.UpdateCommand = SqlDataSource9.UpdateCommand.Replace("@SERIJSKA", "'" + row.Cells[1].Text + "'");
                SqlDataSource9.UpdateCommand = SqlDataSource9.UpdateCommand.Replace("@NALOG", "'" + row.Cells[2].Text + "'");
                //SqlDataSource9.UpdateParameters["PROCESNA"].DefaultValue = procesna;
                //SqlDataSource9.UpdateParameters["SERIJSKA"].DefaultValue = row.Cells[1].Text;
                //SqlDataSource9.UpdateParameters["NALOG"].DefaultValue = row.Cells[2].Text;

                SqlDataSource9.DeleteParameters["PROCESNA"].DefaultValue = procesna;

                if (izhod)
                    SqlDataSource9.Update();
                else
                    SqlDataSource9.Delete();
            }
            
            GridView1.DataBind();
        }






    }
}
